package com.lbx.redis;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import redis.clients.jedis.HostAndPort;

import java.util.*;

/**
 * @description:
 * @author: lbx
 * @create: 2018/7/19 14:18
 **/
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

    @Setter
    @Getter

    private int timeOut;

    @Setter
    @Getter
    private Cluster cluster;

    @Setter
    @Getter
    private String password;



    public static class Cluster {

        @Setter
        @Getter
        private String nodes;

    }


    public  Set<HostAndPort> getClusterNodes(){
        String[] serverArray = cluster.getNodes().split(",");//获取服务器数组(这里要相信自己的输入，所以没有考虑空指针问题)
        Set<HostAndPort> nodes = new HashSet<>();

        for (String ipPort : serverArray) {
            String[] ipPortPair = ipPort.split(":");
            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
        }
        return nodes;
    }

    public Collection<String> getNodes() {
        String[] serverArray = cluster.getNodes().split(",");//获取服务器数组(这里要相信自己的输入，所以没有考虑空指针问题)
        List<String > nodes = new LinkedList<>();
        for (String ipPort : serverArray) {
            nodes.add(ipPort);
        }
        return nodes;
    }

}
